extends "res://BaseEnemy.gd"

# 沙漠骷髅特有属性
@export var projectile_speed = 150.0
@export var projectile_damage = 8
@export var attack_cooldown = 2.0
@export var projectile_range = 300.0

var attack_timer = 0.0
var can_attack = true

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置沙漠骷髅特有属性
	max_health = 30
	current_health = max_health
	movement_speed = 70.0
	damage = 6
	experience_value = 5
	attack_range = 200.0  # 远程攻击范围更大
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(1.0, 0.95, 0.8)  # 骨头色调

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理攻击冷却
	if not can_attack:
		attack_timer += delta
		if attack_timer >= attack_cooldown:
			can_attack = true
			attack_timer = 0.0
	
	# 如果在攻击范围内但不在近战范围内，尝试远程攻击
	if target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= attack_range and distance > 50 and can_attack:
			ranged_attack()

func ranged_attack():
	if animation_player and not animation_player.is_playing():
		if animation_player.has_animation("shoot"):
			animation_player.play("shoot")
			
			# 发射骨头投射物
			var projectile = preload("res://BoneProjectile.tscn").instantiate()
			get_tree().get_root().add_child(projectile)
			projectile.global_position = global_position
			
			# 设置投射物属性
			var direction = (target_player.global_position - global_position).normalized()
			projectile.direction = direction
			projectile.speed = projectile_speed
			projectile.damage = projectile_damage
			projectile.max_range = projectile_range
			projectile.source = self
			
			# 重置攻击冷却
			can_attack = false
			attack_timer = 0.0
			
			print("骷髅发射骨头!")
		else:
			# 如果没有射击动画，使用普通攻击
			super.attack_player()

# 重写攻击方法，近距离时使用近战攻击
func attack_player():
	if global_position.distance_to(target_player.global_position) <= 50:
		# 近战攻击
		if animation_player and not animation_player.is_playing():
			if animation_player.has_animation("melee_attack"):
				animation_player.play("melee_attack")
				
				# 对玩家造成伤害
				if target_player.has_method("take_damage"):
					target_player.take_damage(damage)
			else:
				# 如果没有近战攻击动画，使用普通攻击
				super.attack_player()
	else:
		# 远程攻击由_physics_process处理
		pass
